package business;

import java.util.Vector;

import repository.FrequencyRepository;
import repository.MemberRepository;
import utils.Date;
import classes.Frequency;
import classes.Iterator;
import classes.Member;
import exceptions.RepositoryException;

public class FrequencyBusiness
{
	private FrequencyRepository repository;
	private MemberRepository	memberRepository;
	
	public FrequencyBusiness() throws RepositoryException
	{
		this.repository = FrequencyRepository.getInstance();
		this.memberRepository = MemberRepository.getInstance();
	}
	
	public void add( Frequency frequency) throws RepositoryException
	{
		if( frequency != null )
		{
			repository.add( frequency );
		}
		else
		{
			throw new NullPointerException();
		}
	}
	
	public Iterator< Frequency > find( Frequency frequency ) throws RepositoryException
	{
		return repository.find( frequency );
	}
	
	
	public void edit( Frequency frequency ) throws RepositoryException
	{
		if( frequency != null )
		{
			repository.edit( frequency );
		}
		else
		{
			throw new NullPointerException();
		}
	}
	
	public Frequency get( int id ) throws RepositoryException
	{
		return repository.get( id );
	}
	
	public Iterator< Frequency > getIteratorByPeriod( Date startDate, Date endDate ) throws RepositoryException
	{
		Iterator< Frequency > ret;

		Vector< Frequency > vector = new Vector< Frequency >();
		
		if( startDate.afterMonthDay( endDate ) )
		{
			Date temp = startDate;
			startDate = endDate;
			endDate = temp;
		}
		
		Iterator< Frequency > frequencies = getIterator();
		while( frequencies.hasNext() )
		{
			Frequency frequency = frequencies.next();
			Date frequencyDay = new Date( frequency.getDay().getTime() );
			
			if( frequencyDay.betweenMonthDay( startDate, endDate ) )
			{
				vector.add( frequency );
			}
		}
		
		ret = new Iterator< Frequency >( vector );
		
		return ret;
	}
	
	public Iterator< Frequency > getIteratorByMember( Date startDate, Date endDate, int memberId ) throws RepositoryException
	{
		Iterator< Frequency > ret;

		Vector< Frequency > vector = new Vector< Frequency >();
		
		Iterator< Frequency > frequencies = getIteratorByPeriod( startDate, endDate );
		while( frequencies.hasNext() )
		{
			Frequency frequency = frequencies.next();
			
			if( frequency.getMemberId() == memberId )
			{
				vector.add( frequency );
			}
		}
		
		ret = new Iterator< Frequency >( vector );
		
		return ret;
	}
	
	public Iterator< Frequency > getIteratorByClassroom( Date startDate, Date endDate, int classroomId ) throws RepositoryException
	{
		Iterator< Frequency > ret;

		Vector< Frequency > vector = new Vector< Frequency >();
		
		Iterator< Frequency > frequencies = getIteratorByPeriod( startDate, endDate );
		while( frequencies.hasNext() )
		{
			Frequency frequency = frequencies.next();
			Member member = memberRepository.get( frequency.getMemberId() );	
			if( member.getClassroomId() == classroomId )
			{
				vector.add( frequency );
			}
		}
		
		ret = new Iterator< Frequency >( vector );
		
		return ret;
	}
	
	
	public Iterator< Frequency > getIterator() throws RepositoryException
	{
		return repository.getIterator();
	}
	
	public void remove( int id ) throws RepositoryException
	{
		repository.remove( id );
	}
	
	public int size() throws RepositoryException
	{
		return repository.size();
	}
}
